﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace HangerSupport
{
	public partial class DlgPipeWeightCal : System.Windows.Forms.Form
	{
		public DlgPipeWeightCal(List<SystemLine> _lines)
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
			this.userSetting = new UserSetting();
			this.lines = _lines;
			base.Icon = Resource.YArchitechIcon;
			this.bindGrd();
		}

		private void bindGrd()
		{
			List<PipeWeightData> list = new List<PipeWeightData>();
			foreach (SystemLine systemLine in this.lines)
			{
				double num = 0.0;
				double num2 = 0.0;
				foreach (Pipe item in systemLine.Pipes.Cast<Pipe>().ToList<Pipe>())
				{
					double pipeWeight = this.getPipeWeight(item);
					double warmWeight = this.getWarmWeight(item);
					num += pipeWeight;
					num2 += warmWeight;
					this.totalPipeWeight += pipeWeight;
					this.totalWarmWeight += warmWeight;
				}
				PipeWeightData item2 = new PipeWeightData
				{
					SystemName = systemLine.SystemName,
					PipeWeight = string.Format("{0:F}", Math.Round(num, 2)),
					WarmWeight = string.Format("{0:F}", Math.Round(num2, 2))
				};
				list.Add(item2);
			}
			this.dataGridView1.DataSource = list;
			this.setDefault();
			this.calTotal();
			this.isInt = false;
		}

		private void setDefault()
		{
			this.hyTextBox1.Text = ((this.userSetting.GetValue("PipeWeightCal", "addWeight") == "") ? "0" : this.userSetting.GetValue("PipeWeightCal", "addWeight"));
			this.hyTextBox3.Text = ((this.userSetting.GetValue("PipeWeightCal", "addRatio") == "") ? "10" : this.userSetting.GetValue("PipeWeightCal", "addRatio"));
			string value = this.userSetting.GetValue("PipeWeightCal", "itemizeRatio");
			this.hyTextBox2.Text = ((value == "" || value == "0") ? "1.35" : value);
		}

		private void calTotal()
		{
			this.addWeight = ((this.hyTextBox1.Text.Trim() == "") ? 0.0 : Convert.ToDouble(this.hyTextBox1.Text));
			this.addRatio = ((this.hyTextBox3.Text.Trim() == "") ? 0.0 : Convert.ToDouble(this.hyTextBox3.Text));
			this.itemizeRatio = ((this.hyTextBox2.Text.Trim() == "") ? 0.0 : Convert.ToDouble(this.hyTextBox2.Text));
			this.userSetting.WriteValue("PipeWeightCal", "addWeight", this.addWeight.ToString());
			this.userSetting.WriteValue("PipeWeightCal", "addRatio", this.addRatio.ToString());
			this.userSetting.WriteValue("PipeWeightCal", "itemizeRatio", this.itemizeRatio.ToString());
			this.hyTextBox4.Text = Math.Round((this.totalPipeWeight + this.totalWarmWeight + this.addWeight) * (1.0 + this.addRatio / 100.0) * 9.8 * this.itemizeRatio, 2).ToString();
		}

		private double getWarmWeight(Pipe item)
		{
			string text = item.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_REFERENCE_INSULATION_TYPE).AsString();
			bool flag = text == "" || text == null;
			double result;
			if (flag)
			{
				result = 0.0;
			}
			else
			{
				double num = 70.0;
				bool flag2 = this.warmList.ContainsKey(text);
				if (flag2)
				{
					num = this.warmList[text];
				}
				double num2 = NumbericTools.feetTomm(item.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_OUTER_DIAMETER).AsDouble());
				double num3 = NumbericTools.feetTomm(item.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_REFERENCE_INSULATION_THICKNESS).AsDouble());
				double num4 = NumbericTools.feetTomm(item.GetParameter(Autodesk.Revit.DB.BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble());
				result = 3.14 * (num2 / 1000.0 + num3 / 1000.0) * (num3 / 1000.0) * (num4 / 1000.0) * num;
			}
			return result;
		}

		private double getPipeWeight(Pipe item)
		{
			string text = item.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_MATERIAL_PARAM).AsValueString();
			bool flag = text.Contains("PPR") || text.Contains("铝塑复合") || text.Contains("UPVC") || text.Contains("PEX管") || text.Contains("PB管");
			int type;
			if (flag)
			{
				type = 1;
			}
			else
			{
				bool flag2 = text.Contains("无缝钢管") || text.Contains("钢管") || text.Contains("内外热镀锌钢管");
				if (flag2)
				{
					type = 3;
				}
				else
				{
					type = 4;
				}
			}
			bool flag3 = text.Contains("不锈钢");
			if (flag3)
			{
				type = 2;
			}
			return this.getPipeWeight(item, type);
		}

		private double getPipeWeight(Pipe pipe, int type)
		{
			double dim = NumbericTools.feetTomm(pipe.Diameter);
			Dictionary<double, double> dictionary = null;
			bool flag = type == 1;
			if (flag)
			{
				dictionary = this.PlasticList;
			}
			else
			{
				bool flag2 = type == 2;
				if (flag2)
				{
					dictionary = this.StainlessList;
				}
				else
				{
					bool flag3 = type == 3;
					if (flag3)
					{
						dictionary = this.SteelList;
					}
					else
					{
						bool flag4 = type == 4;
						if (flag4)
						{
							dictionary = this.CopperList;
						}
					}
				}
			}
			foreach (KeyValuePair<double, double> keyValuePair in dictionary)
			{
				bool flag5 = this.isWeight(keyValuePair.Key, dim);
				if (flag5)
				{
					return this.calPipeWeight(keyValuePair.Value, pipe);
				}
			}
			return 0.0;
		}

		private double calPipeWeight(double matchedWeight, Pipe pipe)
		{
			double num = NumbericTools.feetTomm(pipe.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble());
			return matchedWeight * (num / 1000.0);
		}

		private bool isWeight(double formatDim, double dim)
		{
			bool flag = Math.Abs(dim - formatDim) < 0.0001;
			bool result;
			if (flag)
			{
				result = true;
			}
			else
			{
				bool flag2 = dim < formatDim;
				result = flag2;
			}
			return result;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void hyTextBox1_TextChanged(object sender, EventArgs e)
		{
			bool flag = !this.isInt;
			if (flag)
			{
				this.calTotal();
			}
		}

		private List<SystemLine> lines = null;

		private double addWeight = 0.0;

		private double addRatio = 10.0;

		private double itemizeRatio = 1.35;

		private double totalPipeWeight = 0.0;

		private double totalWarmWeight = 0.0;

		private bool isInt = true;

		private UserSetting userSetting = null;

		private Dictionary<double, double> PlasticList = new Dictionary<double, double>
		{
			{
				20.0,
				0.373
			},
			{
				25.0,
				0.564
			},
			{
				32.0,
				0.909
			},
			{
				40.0,
				1.418
			},
			{
				50.0,
				2.209
			},
			{
				63.0,
				3.518
			},
			{
				90.0,
				7.173
			},
			{
				100.0,
				10.573
			},
			{
				125.0,
				13.536
			},
			{
				140.0,
				16.991
			},
			{
				160.0,
				22.0
			},
			{
				200.0,
				34.636
			},
			{
				250.0,
				54.273
			},
			{
				315.0,
				85.636
			}
		};

		private Dictionary<double, double> CopperList = new Dictionary<double, double>
		{
			{
				15.0,
				0.473
			},
			{
				20.0,
				0.925
			},
			{
				25.0,
				1.391
			},
			{
				32.0,
				1.945
			},
			{
				40.0,
				3.045
			},
			{
				50.0,
				4.291
			},
			{
				65.0,
				7.127
			},
			{
				80.0,
				10.791
			},
			{
				100.0,
				15.027
			},
			{
				125.0,
				21.836
			},
			{
				150.0,
				31.0
			},
			{
				200.0,
				59.0
			}
		};

		private Dictionary<double, double> StainlessList = new Dictionary<double, double>
		{
			{
				15.0,
				0.4
			},
			{
				20.0,
				0.755
			},
			{
				25.0,
				1.091
			},
			{
				32.0,
				2.1
			},
			{
				40.0,
				2.455
			},
			{
				50.0,
				4.0
			},
			{
				65.0,
				6.091
			},
			{
				80.0,
				9.991
			},
			{
				100.0,
				13.773
			},
			{
				125.0,
				19.6
			},
			{
				150.0,
				30.055
			}
		};

		private Dictionary<double, double> SteelList = new Dictionary<double, double>
		{
			{
				15.0,
				1.464
			},
			{
				20.0,
				1.991
			},
			{
				25.0,
				2.991
			},
			{
				32.0,
				4.136
			},
			{
				40.0,
				5.136
			},
			{
				50.0,
				7.082
			},
			{
				65.0,
				10.273
			},
			{
				80.0,
				13.427
			},
			{
				100.0,
				19.673
			},
			{
				125.0,
				27.091
			},
			{
				150.0,
				36.909
			},
			{
				200.0,
				65.218
			},
			{
				250.0,
				95.818
			},
			{
				300.0,
				134.182
			},
			{
				350.0,
				183.455
			},
			{
				400.0,
				223.273
			},
			{
				450.0,
				258.818
			},
			{
				500.0,
				321.455
			}
		};

		private Dictionary<string, double> warmList = new Dictionary<string, double>
		{
			{
				"玻璃棉制品",
				90.0
			},
			{
				"超细玻璃棉制品",
				80.0
			},
			{
				"泡沫橡塑制品(PVC/NBR)",
				95.0
			},
			{
				"酚醛泡沫制品(PF)",
				70.0
			},
			{
				"复合硅酸盐制品",
				160.0
			},
			{
				"聚氨酯泡沫制品",
				60.0
			},
			{
				"聚苯乙烯泡沫制品",
				30.0
			},
			{
				"聚乙烯泡沫制品(PEF)",
				50.0
			},
			{
				"岩棉制品",
				200.0
			},
			{
				"泡沫玻璃制品",
				180.0
			},
			{
				"硅酸铝制品",
				192.0
			},
			{
				"微孔硅酸钙制品",
				220.0
			},
			{
				"憎水珍珠岩制品",
				200.0
			}
		};
	}
}
